# Devin Koepl

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

# Set the build type.  Options are:
#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)

rosbuild_init()

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

#uncomment if you have defined messages
#rosbuild_genmsg()
#uncomment if you have defined services
rosbuild_gensrv()

#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
#target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_add_boost_directories()
#rosbuild_link_boost(${PROJECT_NAME} thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example ${PROJECT_NAME})

rosbuild_add_boost_directories()

# List of controllers.
set(CONTROLLERS src/no_controller.cpp src/motor_torque_controller.cpp src/motor_position_controller.cpp src/force_controller.cpp src/torque_sine_controller.cpp src/position_sine_controller.cpp)

# Check to see whether RTAI is installed.
if(EXISTS /usr/realtime)
	# If it is, then build the RT wrappers.

	message("RTAI detected...")

	# Get RTAI compiler flags.
	execute_process(
		COMMAND rtai-config --lxrt-cflags
		OUTPUT_VARIABLE LXRT_CFLAGS
		OUTPUT_STRIP_TRAILING_WHITESPACE
	)
	message("LXRT_CFLAGS: ${LXRT_CFLAGS}")
	execute_process(
		COMMAND rtai-config --lxrt-ldflags
		OUTPUT_VARIABLE LXRT_LDFLAGS
		OUTPUT_STRIP_TRAILING_WHITESPACE
	)
	message("LXRT_LDFLAGS: ${LXRT_LDFLAGS}")
	# Remove the -W strict-prototypes flag, because it is not a valid flag for C++.
	string(REPLACE "-Wstrict-prototypes" "" LXRT_CFLAGS ${LXRT_CFLAGS})

	# Get ECAT compiler flags.
	set(ECAT_CFLAGS "-Wl,--rpath -Wl,/opt/etherlab/lib -L/opt/etherlab/lib -lethercat")
	message("ECAT FLAGS: ${ECAT_CFLAGS}")

	# Add RTAI and ECAT compiler flags.
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LXRT_CFLAGS} ${ECAT_CFLAGS} -lpthread")
	#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LXRT_CFLAGS} ${ECAT_CFLAGS}")
	message("CFLAGS: ${CMAKE_CXX_FLAGS}")

	# Add RTAI libraries to the library search path.
	set(CMAKE_LIBRARY_PATH "${CMAKE_LIBRARY_PATH}:/usr/realtime/lib")

	# Define path for log files, and add it as a definition.
	execute_process(
		COMMAND rospack find fca
		OUTPUT_VARIABLE LOG_FILE_PATH
		OUTPUT_STRIP_TRAILING_WHITESPACE
	)
	set(LOG_FILENAME ${LOG_FILE_PATH}/log_files/fca_log.dat)
	add_definitions(-DLOG_FILENAME=${LOG_FILENAME})

	message("Log Filename: ${LOG_FILENAME}")

	#add_definitions(-DCOMPILE_FOR_RTAI)

	rosbuild_add_executable(fca_interface src/fca_interface.cpp)
	
	rosbuild_add_executable(fca_interface_headless src/fca_interface_headless.cpp)

	include($ENV{ROS_ROOT}/core/rosbuild/FindPkgConfig.cmake)
	pkg_check_modules(GTKMM REQUIRED gtkmm-2.4)
	include_directories(${GTKMM_INCLUDE_DIRS})
	link_directories(${GTKMM_LIBRARY_DIRS})

	target_link_libraries(fca_interface ${GTKMM_LIBRARIES})

	# XXX Fix and re-include.
	#rosbuild_add_executable(rt_fca_controller_wrapper src/rt_fca_controller_wrapper.cpp src/controller_wrapper_states.cpp src/control_switcher_state_machine.cpp ${CONTROLLERS})
else(EXISTS /usr/realtime)
	message("RTAI not detected.  Your system is broken.")
endif(EXISTS /usr/realtime)



#rosbuild_add_executable(tester src/tester.cpp src/data_logger.cpp)

